import datetime

import string

class FormChecker :
	def isValidEmail(self, addr) :
		if (len(addr) == 0) : return False
		rfc822_specials = '()<>,;:\\"[]'
		c = 0
		while (c < len(addr)) :
			if (ord(addr[c]) < 32 or ord(addr[c]) >= 127) : return False
			if (addr[c] in rfc822_specials) : return False
			if (addr[c] == '@') : break
			c += 1
		if (addr[c] != '@') : return False
		if (c == 0 or addr[c - 1] == '.') : return False
		c += 1
		
		domain = c
		if (domain >= len(addr)) : return False
		count = 0
		while (c < len(addr)) :
			if (addr[c] == '.') :
				if (addr[c - 1] == '@' or addr[c - 1] == '.') : return False
				count += 1
			if (addr[c] in rfc822_specials or addr[c] == '@'): return False
			c += 1
		if (addr[c - 1] == '.') : return False
		return (count >= 1)
	    
	def uLen(self, s) :
		ret = 0
		for i in range(0, len(s)) :
			if (ord(s[i]) > 127) : ret += 2
			else : ret += 1
		return ret
	
	def isValidFidocard(self, number) :
		if (len(number) != 13) : return False
		for i in range(0, len(number)) :
			if (number[i] > '9' or number[i] < '0') : return False
		return True
	
	def isValidMobile(self, number) :
		if (len(number) != 11) : return False
		for i in range(0, len(number)) :
			if (number[i] > '9' or number[i] < '0') : return False
		return True
	
	def isValidName(self, name) :
		if (self.uLen(name) < 4 or self.uLen(name) > 16) : return False
		for i in range(0, len(name)) :
			c = name[i]
			if (not ((ord(c) <= 0x9fff and ord(c) >= 0x4e00) or (c <= '9' and c >= '0') or (c <= 'z' and c >= 'a') or 
				(c <= 'Z' and c >= 'A') or c == ' ')) :
					return False
		return True
		
	def isValidPassword(self, password) :
		if (self.uLen(password) < 6 or self.uLen(password) > 16) : return False
		for i in range(0, len(password)) :
			c = password[i]
			if (not ((c <= 'Z' and c >= 'A') or (c <= 'z' and c >= 'a') or (c <= '9' and c >= '0') or c == ' ' or c == '.')) :
				return False
		return True
		
	def isValidText(self, text) :
		for i in range(0, len(text)) :
			c = text[i]
			if (ord(c) < 32 or c == '"' or
				(c == '<' and (i + 4 >= len(text) or text[i + 1] != 'b' or text[i + 2] != 'r' or text[i + 3] != '/' or text[i + 4] != '>'))
				or c == ' ') : return False
		return True
		
	def strToInt(self, s) :
		try :
			ret = int(s)
			return ret
		except :
			return None
	
	def strToDate(self, s) :
		try :
			ret = datetime.datetime.strptime(s, "%Y-%m-%d")
		except :
			return None
		return ret